<template>
  <div class="container">
    <div class="app-container">
      <el-tree default-expand-all :data="depts" :props="defaultProps" :expand-on-click-node="false">
        <template #default="{data}">

          <el-row type="flex" style="width: 100%;" justify="space-between" align="middle">
            <el-col :span="4">{{ data.name }}</el-col>
            <el-col :span="4">
              <span class="tree-man">{{ data.managerName }}</span>
              <el-dropdown @command="handleCommand($event,data.id)">
                <span class="el-dropdown-link">
                  操作<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                  <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                  <el-dropdown-item command="del">删除部门</el-dropdown-item>

                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </template>

      </el-tree>
    </div>
    <TreeDialog ref="treeDialog" :dialog-visible.sync="dialogVisible" :select-node-id="selectNodeId" @updateTree="updateTree" />
  </div>
</template>
<script>
import { getDepartmentList, delDepartment } from '@/api/department'
import { queryListBytree } from '@/utils/index'
import TreeDialog from '@/views/department/components/Tree-Dialog.vue'
export default {
  name: 'Department',
  components: {
    TreeDialog
  },
  data() {
    return {
      selectNodeId: null,
      depts: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },

      // 点击部门弹出框状态
      dialogVisible: false

    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      try {
        const res = await getDepartmentList()
        const arr = queryListBytree(res, 0)
        this.depts = arr
      } catch (error) {
        console.log(error)
      }
    },

    async handleCommand(type, id) {
      if (type === 'add') {
        this.dialogVisible = true
        this.selectNodeId = id
      } else if (type === 'edit') {
        // 打开弹窗
        this.dialogVisible = true
        // 获取当前点击的nodeID 赋值给子节点
        // 由于vue对数据的操作是异步的  这里需等待
        this.selectNodeId = id
        // 调用子组件的getTreeNodeDetail方法
        this.$nextTick(() => {
          this.$refs.treeDialog.getTreeNodeDetail()
        })
      } else {
        // delDepartment
        this.$confirm('此操作将永久删除该部门, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '再想想',
          type: 'warning'
        }).then(async() => {
          await delDepartment(id)
          this.$message.success('删除部门成功')
          this.getDepartments()
        })
      }
    },

    // 更新树组件
    updateTree() {
      this.getDepartments()
    }
  }

}
</script>

<style scoped>
.app-container{
  padding: 30px 140px;
  font-size: 14px;
}
.tree-man{
  width: 50px;
  margin-right: 20px;
  display: inline-block;
}
</style>
